﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_CategoryAdmin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack){
        loadData();
        }
    }
    protected void grvCategories_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = e.Keys[0].ToString();
        BookStoreDataContext bs = new BookStoreDataContext();
        var deleteCate = bs.categories.Single(book => book.category_id.ToString() == id);
        //bs.categories.DeleteOnSubmit(deleteBook);


        bs.SubmitChanges();
        grvCategories.DataBind();
    }

    void deleteProductByCategoryID(int id)
    {
        BookStoreDataContext bs = new BookStoreDataContext();
        var deleteBooks = (from book in bs.items where book.category_id == id select book);
    }
    protected void btnAddCategory_Click(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
           

        }

    }
    protected void msgBox1_Disposed(object sender, EventArgs e)
    {
        string valueConfirm = Request.Form["hdf_1"];
    }
    protected void grvCategories_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void grvCategories_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvCategories.EditIndex = e.NewEditIndex;
        loadData();
    }
    public void loadData() {
        BookStoreDataContext bs = new BookStoreDataContext();
        var list = bs.categories.ToList();
        grvCategories.DataSource = list;
        grvCategories.DataBind();
    }
    protected void grvCategories_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = e.Keys[0].ToString();
        string newName = this.grvCategories.Rows[e.RowIndex].Cells[1].Text;
        BookStoreDataContext bs = new BookStoreDataContext();
        var updateCate = bs.categories.Single(c => c.category_id.ToString() == id);
        if(updateCate != null){
            updateCate.name = newName;
            bs.SubmitChanges();
        }
    }
}